﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using pub.Interop;

namespace scheduler
{
    public class MessageSinkWindow : NativeWindow
    {
        public MessageSinkWindow()
        {
            CreateHandle(new CreateParams {
                Caption = Consts.MessageSinkWindowTitle,
                ClassName = "Message",
                Parent = Win32.HWND_MESSAGE
            });
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Consts.WM_TEST_MSG)
            {
                MessageBox.Show("WM_TEST_MSG...");
            }
            else if (m.Msg == Consts.WM_APP_ACTIVATE)
            {
                App.MainForm.ShowUp();
            }
            else if (m.Msg == Win32.WM_COPYDATA)
            {
                var cds = (Win32.COPYDATASTRUCT)Marshal.PtrToStructure(
                    m.LParam, typeof(Win32.COPYDATASTRUCT));
                ProcessCopyData((int) cds.dwData, cds.cbData, cds.lpData);
            }
            base.WndProc(ref m);
        }

        private void ProcessCopyData(int type, int byteCount, string lpData)
        {
            if (type == Consts.COPY_DATA_TYPE_ADD)
            {
//                MessageBox.Show("ADD: " + lpData);
                App.MainForm.AddTodoList(lpData);
            }
        }
    }
}